Completed
Push — development ( 6352e5...8bea34 )
by Nils
07:55
created

upgrade.js ➔ ... ➔ xhr_object.onreadystatechange   C

Complexity

Conditions 10
Paths 12

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 12
nop 0
dl 0
loc 23
rs 5.6534
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like upgrade.js ➔ ... ➔ xhr_object.onreadystatechange often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/**
2
 * @file 		  upgrade.js
3
 * @author        Nils Laumaillé
4
 * @version       2.1.27
5
 * @copyright     (c) 2009-2011 Nils Laumaillé
6
 * @licensing     GNU AFFERO GPL 3.0
7
 * @link          http://www.teampass.net
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 */
13
14
// Function - do a pause during javascript execution
15
function PauseInExecution(millis)
16
{
17
    var date = new Date();
18
    var curDate = null;
0 ignored issues
show
Unused Code introduced by
The assignment to curDate seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
19
20
    do { curDate = new Date(); }
21
    while(curDate-date < millis);
22
}
23
24
//Fonction qui permet d'appeler un fichier qui ex�cute une requete pass�e en parametre
25
function httpRequest(file,data,type) {
26
    var xhr_object = null;
27
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
28
29
    if (document.getElementById("menu_action") != null) {
30
        document.getElementById("menu_action").value = "action";
31
    }
32
33
    if(window.XMLHttpRequest) { // Firefox
34
        xhr_object = new XMLHttpRequest();
35
    } else if(window.ActiveXObject) { // Internet Explorer
36
        xhr_object = new ActiveXObject("Microsoft.XMLHTTP");  //Info IE8 now supports =>  xhr_object = new XMLHttpRequest()
37
    } else { // XMLHttpRequest non support? par le navigateur
38
        alert("Your browser does not support XMLHTTPRequest objects ...");
39
        return;
40
    }
41
42
    if (type == "GET") {
43
        xhr_object.open("GET", file+"?"+data, true);
44
        xhr_object.send(null);
45
    } else {
46
        xhr_object.open("POST", file, true);
47
        xhr_object.onreadystatechange = function() {
48
            if(xhr_object.readyState == 4) {
49
                eval(xhr_object.responseText);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
50
                //Check if query is for user identification. If yes, then reload page.
51
                if (data != "" && data !== undefined && data.indexOf('ype=identify_user') > 0 ) {
52
                    if (is_chrome === true ) PauseInExecution(100);  //Needed pause for Chrome
53
                    if (type == "") {
54
                        if (document.getElementById('erreur_connexion').style.display == "") {
55
                            //rise an error in url. This in order to display the eror after refreshing
56
                            window.location.href="index.php?error=rised";
57
                        } else {
58
                            window.location.href="index.php";
59
                        }
60
                    } else {
61
                        if (type = "?error=rised") {
62
                            if (document.getElementById('erreur_connexion').style.display == "none") type = "";   //clean error in url
63
                            else type = "?error=rised"; //Maintain the ERROR
64
                        }
65
                        window.location.href="index.php"+type;
66
                    }
67
                }
68
            }
69
        }
70
        xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
71
        xhr_object.send(data);
72
    }
73
}